import type { VxeGridProps } from '#/adapter/vxe-table';
import type { FormSchemaGetter } from '#/adapter/form';
import dayjs from 'dayjs';
import { z } from '#/adapter/form';
export const columns: VxeGridProps['columns'] =
[
    { title: '用户名', field: 'userName' },
    { title: '姓名', field: 'name' },
    { title: '别名', field: 'surname' },
    { title: '邮箱', field: 'email' },
    { title: '电话', field: 'phoneNumber' },
    {
      title: '最后修改时间',
      field: 'lastModificationTime',
      formatter: ({ cellValue }) => {
        return dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss');
      },
    },
    {
      title: '创建时间',
      field: 'creationTime',
      formatter: ({ cellValue }) => {
        return dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss');
      },
    },
    {
      field: 'action',
      fixed: 'right',
      slots: { default: 'action' },
      title: '操作',
      resizable: false,
      width: '200',
    },
  ];
export const querySchema: FormSchemaGetter = () => [
    {
      component: 'Input',
      fieldName: 'filter',
      label: '用户信息',
    }
  ];


  export const modalSchema: FormSchemaGetter = () => [
    {
      component: 'Input',
      dependencies: {
        show: () => false,
        triggerFields: [''],
      },
      fieldName: 'id',
      label: '主键',
    },
    {
      component: 'Input',
      fieldName: 'userName',
      label: '用户名',
      rules: z.string().min(1, { message: '请输入用户名' }),
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: '姓名',
      rules: z.string().min(1, { message: '请输入姓名' }),
    },
    {
      component: 'Input',
      fieldName: 'surname',
      label: '别名',
      rules: z.string().optional(), // 姓氏通常是可选的
    },
    {
      component: 'Input',
      fieldName: 'email',
      label: '邮箱',
      rules: z.string().email({ message: '请输入正确的邮箱格式' }).min(1, { message: '请输入邮箱' }),
    },
    {
      component: 'Input',
      fieldName: 'phoneNumber',
      label: '电话',
      rules: z.string().regex(/^1[3-9]\d{9}$/, { message: '请输入正确的手机号码' }).optional().or(z.literal('')),
    },
    {
      component: 'InputPassword',
      fieldName: 'password',
      label: '密码',
      rules: z.string().optional(), // 默认可选，实际规则在 dependencies 中处理
      dependencies: {
        triggerFields: ['id', 'password'],
        rules: (value: Partial<Record<string, any>>) => {
          const passwordSchema = z.string()
            .min(6, { message: '密码至少需要6个字符' })
            .regex(/[a-z]/, { message: '密码必须包含至少一个小写字母' })
            .regex(/[A-Z]/, { message: '密码必须包含至少一个大写字母' })
            .regex(/[^a-zA-Z0-9]/, { message: '密码必须包含至少一个特殊字符' });

          if (value && value.id) {
            // 编辑模式，如果输入了密码，则校验复杂度，否则可选
            return value.password ? passwordSchema : z.string().optional();
          } else {
            // 新增模式，密码必填并校验复杂度
            return passwordSchema;
          }
        },
      },
    },
    {
      component: 'Switch', // 使用开关组件
      fieldName: 'isActive',
      label: '是否启用',
      defaultValue: true, // 默认启用
      rules: z.boolean(),
    },
    {
      component: 'Switch', // 使用开关组件
      fieldName: 'lockoutEnabled',
      label: '锁定启用',
      defaultValue: false, // 默认不锁定
      rules: z.boolean(),
    },
    {
      // 修改为 CheckboxGroup 以支持多选
      component: 'CheckboxGroup',
      fieldName: 'roleNames',
      label: '角色',
      rules: z.array(z.string()).optional(),
      componentProps: {
        options: [], // 动态加载
      },
    },
  ];
